<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false" width="71%" :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" label-width="120px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="dataForm.createTime" :disabled="disabled" placeholder="创建时间" type="date" value-format="yyyy-MM-dd HH:mm:SS" clearable/>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-date-picker v-model="dataForm.updateTime" :disabled="disabled" placeholder="修改时间" type="date" value-format="yyyy-MM-dd HH:mm:SS" clearable/>
      </el-form-item>
      <el-form-item label="产品Id" prop="productId">
        <el-input v-model="dataForm.productId" :disabled="disabled" placeholder="产品Id" clearable/>
      </el-form-item>
      <el-form-item label="产品名" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" placeholder="产品名" clearable/>
      </el-form-item>
      <el-form-item label="产品类型id" prop="typeId">
        <el-input v-model="dataForm.typeId" :disabled="disabled" placeholder="产品类型id" clearable/>
      </el-form-item>
      <el-form-item label="产品描述" prop="desc">
        <el-input v-model="dataForm.desc" :disabled="disabled" placeholder="产品描述" clearable/>
      </el-form-item>
      <el-form-item label="产品图列表" prop="imageList">
        <el-input v-model="dataForm.imageList" :disabled="disabled" placeholder="产品图列表" clearable/>
      </el-form-item>
      <el-form-item label="hscode" prop="hsCode">
        <el-input v-model="dataForm.hsCode" :disabled="disabled" placeholder="hscode" clearable/>
      </el-form-item>
      <el-form-item label="产品报价" prop="price">
        <el-input v-model="dataForm.price" :disabled="disabled" placeholder="产品报价" clearable type="number"/>
      </el-form-item>
      <el-form-item label="价格单位" prop="priceUnit">
        <el-input v-model="dataForm.priceUnit" :disabled="disabled" placeholder="价格单位" clearable/>
      </el-form-item>
      <el-form-item label="产品规格" prop="specification">
        <el-input v-model="dataForm.specification" :disabled="disabled" placeholder="产品规格" clearable/>
      </el-form-item>
      <el-form-item label="产品规格图片列表" prop="specificationImageList">
        <el-input v-model="dataForm.specificationImageList" :disabled="disabled" placeholder="产品规格图片列表" clearable/>
      </el-form-item>
      <el-form-item label="产品包装规格" prop="pack">
        <el-input v-model="dataForm.pack" :disabled="disabled" placeholder="产品包装规格" clearable/>
      </el-form-item>
      <el-form-item label="最少起订数量" prop="minOrderNum">
        <el-input v-model="dataForm.minOrderNum" :disabled="disabled" placeholder="最少起订数量" clearable type="number"/>
      </el-form-item>
      <el-form-item label="最少起订数量单位" prop="minOrderNumUnit">
        <el-input v-model="dataForm.minOrderNumUnit" :disabled="disabled" placeholder="最少起订数量单位" clearable/>
      </el-form-item>
      <el-form-item label="文件地址" prop="fileUrl">
        <el-input v-model="dataForm.fileUrl" :disabled="disabled" placeholder="文件地址" clearable/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-input v-model="dataForm.status" :disabled="disabled" placeholder="状态" clearable/>
      </el-form-item>
      <el-form-item label="是否装饰" prop="isDisplay">
        <el-input v-model="dataForm.isDisplay" :disabled="disabled" placeholder="是否装饰" clearable/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: '',
          createTime: '',
          updateTime: '',
          productId: '',
          name: '',
          typeId: '',
          desc: '',
          imageList: '',
          hsCode: '',
          price: '',
          priceUnit: '',
          specification: '',
          specificationImageList: '',
          pack: '',
          minOrderNum: '',
          minOrderNumUnit: '',
          fileUrl: '',
          status: '',
          isDisplay: ''},
        dataRule: {
          createTime: [{required: true, message: '创建时间不能为空', trigger: 'blur'}],
          updateTime: [{required: true, message: '修改时间不能为空', trigger: 'blur'}],
          productId: [{required: true, message: '产品Id不能为空', trigger: 'blur'}],
          name: [{required: true, message: '产品名不能为空', trigger: 'blur'}],
          typeId: [{required: true, message: '产品类型id不能为空', trigger: 'blur'}],
          desc: [{required: true, message: '产品描述不能为空', trigger: 'blur'}],
          imageList: [{required: true, message: '产品图列表不能为空', trigger: 'blur'}],
          price: [{required: true, message: '产品报价不能为空', trigger: 'blur'}],
          priceUnit: [{required: true, message: '价格单位不能为空', trigger: 'blur'}],
          status: [{required: true, message: '状态不能为空', trigger: 'blur'}],
          isDisplay: [{required: true, message: '是否装饰不能为空', trigger: 'blur'}],
          other: []
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/gana/product/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.info
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/gana/product/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({ message: '操作成功', type: 'success', duration: 1500 })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
